package com.philip.journal.core.bean;

import com.philip.journal.core.exception.JournalException;

/**
 * Represents a user of the Journal Application.
 *
 * This bean is tied with spring hibernate. Will disallow setting the username from the client code.
 */
public class User extends AbstractAuditableBean {

    /** RTFC. */
    private String username;
    /** RTFC. */
    private String password;
    /** Used for maintaining the user entity integrity. */
    private String checksum;

    /** Default constructor. */
    public User() {
        super();
    }

    /**
     * RTFC.
     *
     * @param pusername
     *            rtfc.
     */
    public User(final String pusername) {
        this();
        this.username = pusername;
    }

    /**
     * RTFC.
     *
     * @param pusername
     *            username
     * @param ppassword
     *            password
     * @param pchecksum
     *            used for maintaining integrity of data.
     */
    public User(final String pusername, final String ppassword, final String pchecksum) {
        this();
        this.username = pusername;
        this.password = ppassword;
        this.checksum = pchecksum;
    }

    /**
     * Accessor method.
     *
     * @return username.
     */
    public String getUsername() {
        return this.username;
    }

    /** Hibernate specific check to make sure hibernate module is setting our property. */
    static final int IDX_HBN_CALL = 6;

    /**
     * This should called only by hibernate and not by client code. Users created by client should be created instead by
     * the provided parameterized constructor.
     *
     * @param pusername username to set.
     * @exception JournalException - Thrown when this is invoked by developer from any client.
     *
     */
    public void setUsername(final String pusername) {
        final String callingClass = Thread.currentThread().getStackTrace()[IDX_HBN_CALL].getClassName();
        if (callingClass.indexOf("org.hibernate.property.BasicPropertyAccessor$BasicSetter") > -1
                || callingClass.indexOf("org.hibernate.tuple.entity.AbstractEntityTuplizer") > -1) {
            this.username = pusername;
        } else {
            throw new JournalException("Illegal access to protected object", null);
        }
    }

    /**
     * Accessor method.
     *
     * @return the password.
     */
    public String getPassword() {
        return this.password;
    }

    /**
     * Accessor method.
     *
     * @param ppassword
     *            password to set.
     */
    public void setPassword(final String ppassword) {
        this.password = ppassword;
    }

    /**
     * Accessor method.
     *
     * @return checksum.
     */
    public String getChecksum() {
        return this.checksum;
    }

    /**
     * Accessor method.
     *
     * @param pchecksum
     *            checksum to set.
     */
    public void setChecksum(final String pchecksum) {
        this.checksum = pchecksum;
    }

    @Override
    public String getPrimaryKeyField() {
        return "username";
    }
}
